package view.Arm
{
	import data.ArmsProp;
	import data.Globals;
	import data.TankProp;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.utils.getTimer;
	
	import manager.EffectName;
	import manager.ObjDataManage;
	import manager.ObjectPool;
	
	import utils.GameUtils;
	
	import view.base.BaseView;
	import view.plant.PlantItem;
	
	public class BaseTank extends BaseView
	{
		private var _bulletType:String;
		
		private var _shoutRadius:int;
		
		private var _shoutGap:int;
		
		protected var _blood:int;
		
		private var bloodBar:BloodBarCls;
		
		protected var preTime:Number;
		
		private var _rotateSpeed:Number;
		/**
		 * 射击对象 
		 */		
		public var shoutTarget:DisplayObject;
		
		private var monsterAry:Array = Globals.gameView.getMonsterAry();
		
		private var cur:Number;
		
		private var next:Number;
		
		public var onRemove:Function;
		
		protected var disObj:DisplayObject;
		
		protected var cls:Class;
		
		public function BaseTank()
		{
			super();
			disObj = new cls();
			this.addChild(disObj);
			bloodBar = new BloodBarCls();
			bloodBar.mouseChildren = false;
			bloodBar.mouseEnabled = false;
			this.addChild(bloodBar);
			bloodBar.stop();
			bloodBar.y = 10;
			bloodBar.visible = false;
			disObj.scaleX = 40/disObj.height;
			disObj.height = 40;
		}
		
		override protected function loop(event:Event):void{
			if(isCloseLoop){
				return;
			}
			if(shoutTarget){
				var distance:int = GameUtils.getTwoPointDistance(new Point(shoutTarget.x,shoutTarget.y),new Point(this.x,this.y));
				if(distance > shoutRadius){
					shoutTarget=null;
				}else{
					chechTargetIsDie();
				}
			}
			if(!shoutTarget)findTarget();
			if(shoutTarget){
				turnDirection();
			}
		}
		
		/**
		 *检测目标是否已经死亡
		 */
		protected function chechTargetIsDie():void
		{
			if(shoutTarget is PlantItem){
				if(Globals.gameView.targetPlant != shoutTarget){
					shoutTarget = null;
				}
			}else{
				if(monsterAry.indexOf(shoutTarget)==-1){
					shoutTarget = null;
				}
			}
		}
		
		protected function turnDirection():void
		{
			cur = this.rotation;
			next = GameUtils.getRotation(new Point(this.x,this.y),new Point(shoutTarget.x,shoutTarget.y));
			
			if(next>180){
				next = next-360;
			}
			
			var gap:int =  Math.abs(next-cur);
			if(gap<rotateSpeed*2+1){
				shout();
			}else{
				if(gap<180){
					if(next>cur){
						gap<rotateSpeed ? this.rotation+=gap : this.rotation+=rotateSpeed;
					}else{
						gap<rotateSpeed ? this.rotation-=gap : this.rotation-=rotateSpeed;
					}
				}else{
					if(cur<next){
						gap<rotateSpeed ? this.rotation-=gap : this.rotation-=rotateSpeed;
					}else{
						gap<rotateSpeed ? this.rotation+=gap : this.rotation+=rotateSpeed;
					}
				}
			}
		}
		
		protected function shout():void{
			var isShoot:Boolean;
			if(preTime){
				var curUse:Number = getTimer() - preTime;
				if(curUse > shoutGap){
					preTime = getTimer();
					isShoot = true;
				}
			}else{
				preTime = getTimer();
				isShoot = true;
			}
			if(isShoot){
				Globals.gameView.shoutBullet(bulletType,new Point(this.x,this.y),new Point(shoutTarget.x,shoutTarget.y),null,0,0,shoutTarget);
			}
		}
		
		protected function findTarget():void
		{
			var distance:int=0;
			var display:DisplayObject
			display = Globals.gameView.targetPlant;
			if(display){
				distance = GameUtils.getTwoPointDistance(new Point(display.x,display.y),new Point(this.x,this.y));
				if(distance<=shoutRadius){
					shoutTarget = display;
					return;
				}
			}
			display = Globals.gameView.targetMonster;
			if(display){
				distance = GameUtils.getTwoPointDistance(new Point(display.x,display.y),new Point(this.x,this.y));
				if(distance<=shoutRadius){
					shoutTarget = display;
					return;
				}
			}
			for(var i:int=0;i<monsterAry.length;i++){
				distance = GameUtils.getTwoPointDistance(new Point(monsterAry[i].x,monsterAry[i].y),new Point(this.x,this.y));
				if(distance<=shoutRadius){
					shoutTarget = monsterAry[i];
					return;
				}else{
					continue;
				}
			}
			shoutTarget = null;
		}
		
		public function die():void{
			if(this.parent){
				this.parent.removeChild(this);
				isCloseLoop = true;
				shoutTarget = null;
				bloodBar.visible = false;
				ObjectPool.putObj(this);
				try{
					Globals.gameView.createExplode(EffectName.A_EXPLODE,this.x,this.y);
				}catch(e:TypeError){
					
				}
			}
			onRemove(this);
		}
		
		/**
		 * 射击半径 
		 * @return 
		 * 
		 */
		public function get shoutRadius():int
		{
			return _shoutRadius;
		}
		
		public function set shoutRadius(value:int):void
		{
			_shoutRadius = value;
		}
		
		/**
		 * 子弹发射频率 间隔为毫秒 
		 */
		public function get shoutGap():int
		{
			return _shoutGap;
		}
		
		/**
		 * @private
		 */
		public function set shoutGap(value:int):void
		{
			_shoutGap = value;
		}
		
		public function get bulletType():String
		{
			return _bulletType;
		}
		
		public function set bulletType(value:String):void
		{
			_bulletType = value;
		}
		
		public function get rotateSpeed():Number
		{
			return _rotateSpeed;
		}
		
		public function set rotateSpeed(value:Number):void
		{
			_rotateSpeed = value;
		}
		
		public function get blood():int
		{
			return _blood;
		}
		
		public function set blood(value:int):void
		{
			_blood = value;
			var tp:ArmsProp = ObjDataManage.getInstance().getTankProp(this.name);
			bloodBar.gotoAndStop(int((tp.blood-value)/tp.blood*bloodBar.totalFrames));	
			if(value<tp.blood){
				bloodBar.visible = true;
			}else{
				bloodBar.visible = false;
			}
			if(value<=0){
				die();
			}
		}
		
		
	}
}